Python Programming Guide For Beginners: A Simple Introduction to Python Programming by Phillips Gerald

Python Programming Guide For Beginners: A Simple Introduction to Python Programming by Phillips Gerald

Author:Phillips, Gerald [Phillips, Gerald]
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2019-10-08T16:00:00+00:00


Lists

This is a sequence type defined by the list class in Python and it allows you to add, delete, and process elements easily.

Creating a list

Syntax: >>> my_list = [1,2,3,4,5]

Elements in a list can be of the same or different types. For example,

mixed_list = [“my mixed list”, 12, 33.54]

Other ways of creating a list are as follows:

list1 = list() # Create an empty list

list2 = list([22, 31, 61]) # Create a list with elements 22, 31, 61

list3 = list(["tom", "jerry", "spyke"]) # Create a list with strings

list5 = list("python") # Create a list with characters p, y, t, h, o, n

NOTE: Lists are mutable , meaning their content can be changed once created.

Accessing List Elements

List elements can be accessed using the index operator ( [] ), with the index starting from 0.

>>> my_list = [1,2,3,4,5]

>>> my_list [1] # access second element in the list 2

>>> my_list [0] # access first element in the list 1

Common List Operations

Method Name Description

x in s True if element x is in sequence s

x not in s True if element x is not in sequence s

s1 + s2 Concatenates two sequences s1 and s2

s * n , n * s n copies of sequence s concatenated

s[i] ith element in sequence s

len(s) Length of sequence s, i.e. the number of elements in s

min(s) Smallest element in sequence s

max(s) Largest element in sequence s

sum(s) Sum of all numbers in sequence s

for loop Traverses elements from left to right in a for loop

List Slicing

The slice operator ( [start:end] ) allows to fetch sublist from the list. It works similar to string explained in the previous chapter.

Note: If start >= end , list[start : end] will return an empty list. If end specifies a position which is beyond the end of the list, Python will use the length of the list for end instead.

Commonly Used List Methods with Return Type

Method Name Description

append(x:object):None

Adds an element x to the end of the list and returns None

count(x:object):int

Returns the number of times element x appears in the list

extend(l:list):None

Appends all the elements in l to the list and returns None

index(x: object):int

Returns the index of the first occurrence of element x in the list

insert(index: int, x: object):None

Inserts an element x at a given index. Note that the first element in the list has index 0 and returns None

remove(x:object):None

Removes the first occurrence of element x from the list and returns None

reverse():None Reverse the list and returns None

sort(): None

Sorts the elements in the list in ascending order and returns None

spop(i): object

Removes the element at the given position and returns it. The parameter i is optional. If it is not specified, pop() removes and returns the last element in the list.

List Comprehension

Note: This topic needs to have a working knowledge of python for loops.

List comprehension provides a concise way to create lists. It consists of square brackets containing an expression followed by a for clause then zero or more if clauses.

Below are some examples:

>>> list1 = [ x for x in range(10) ] >>> list1

[0, 1, 2, 3, 4,



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.